package com.demo.connectdbtest.module.exportrecord.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.demo.connectdbtest.module.common.util.DateUtils;
import com.demo.connectdbtest.module.common.util.ExportExcelUtil;
import com.demo.connectdbtest.module.exportrecord.domain.TempLiveStat;
import com.demo.connectdbtest.module.exportrecord.popoMapper.TempLiveStatMapper;
import com.demo.connectdbtest.module.exportrecord.service.ExportService;
import com.demo.connectdbtest.module.exportrecord.vo.LiveNumVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 描述：
 *
 * @author: chengc
 * @date 2022/03/28
 */
@Service
public class ExportServiceImpl implements ExportService {
    private Logger log = LoggerFactory.getLogger(ExportServiceImpl.class);

    @Autowired
    private TempLiveStatMapper tempLiveStatMapper;

    /**
     * 导出不同时间点的直播人数
     *
     * @param startTime
     * @param endTime
     */
    @Override
    public void exportLiveNum(Long startTime, Long endTime, HttpServletResponse response) {
        String start = null;
        String end = null;
        if (Objects.nonNull(startTime)) {
            start = DateUtils.format(new Date(startTime), DateUtils.DATEFORMATDAY);
        }
        if (Objects.nonNull(endTime)) {
            end = DateUtils.format(new Date(endTime), DateUtils.DATEFORMATDAY);
        }

        List<TempLiveStat> respList = tempLiveStatMapper.selectByTime(start, end);
        log.debug("直播记录result={}", JSONObject.toJSONString(respList));
        if (CollectionUtils.isEmpty(respList)) {
            return;
        }
        List<TempLiveStat> list = respList.stream().sorted(Comparator.comparing(TempLiveStat::getDay).reversed()).collect(Collectors.toList());
        List<LiveNumVo> exportData = new ArrayList<>();
        List<String> dayList = list.stream().map(TempLiveStat::getDay).distinct().collect(Collectors.toList());
        Map<String, List<TempLiveStat>> map = list.stream().collect(Collectors.groupingBy(TempLiveStat::getDay));

        // List<String> dayList = list.stream().map(item -> DateUtils.format(new Date(item.getStartTime()), DateUtils.DATEFORMATDAY)).distinct().collect(Collectors.toList());
        if (!map.isEmpty() && !CollectionUtils.isEmpty(dayList)) {
            Set<String> daySet = map.keySet();
            daySet.forEach(day -> {
                List<TempLiveStat> liveRecordVoList = map.get(day);
                LiveNumVo respItem = new LiveNumVo();
                if (!CollectionUtils.isEmpty(liveRecordVoList)) {
                    Integer zeroClock = 00;
                    Integer oneClock = 1;
                    Integer twoClock = 2;
                    Integer threeClock = 3;
                    Integer fourClock = 4;
                    Integer fiveClock = 5;
                    Integer sxiClock = 6;
                    Integer sevenClock = 7;
                    Integer eightClock = 8;
                    Integer nineClock = 9;
                    Integer tenClock = 10;
                    Integer elevenClock = 11;
                    Integer twelveClock = 12;
                    Integer thirteenClock = 13;
                    Integer fourteenClock = 14;
                    Integer fiveteenClock = 15;
                    Integer sixteenClock = 16;
                    Integer sevenTeenClock = 17;
                    Integer eighteenClock = 18;
                    Integer nineteenClock = 19;
                    Integer twentyClock = 20;
                    Integer twentyOneClock = 21;
                    Integer twentyTwoClock = 22;
                    Integer twentyThreeClock = 23;

                    long zeroTotal = liveRecordVoList.stream().filter(item -> zeroClock.equals(item.getOne()) || zeroClock.equals(item.getTwo()) || zeroClock.equals(item.getThree())
                            || zeroClock.equals(item.getFour()) || zeroClock.equals(item.getFive()) || zeroClock.equals(item.getSix()) || zeroClock.equals(item.getSeven())
                            || zeroClock.equals(item.getEight()) || zeroClock.equals(item.getNine())).count();

                    long oneTotal = liveRecordVoList.stream().filter(item -> oneClock.equals(item.getOne()) || oneClock.equals(item.getTwo()) || oneClock.equals(item.getThree())
                            || oneClock.equals(item.getFour()) || oneClock.equals(item.getFive()) || oneClock.equals(item.getSix()) || oneClock.equals(item.getSeven())
                            || oneClock.equals(item.getEight()) || oneClock.equals(item.getNine())).count();

                    long twoTotal = liveRecordVoList.stream().filter(item -> twoClock.equals(item.getOne()) || twoClock.equals(item.getTwo()) || twoClock.equals(item.getThree())
                            || twoClock.equals(item.getFour()) || twoClock.equals(item.getFive()) || twoClock.equals(item.getSix()) || twoClock.equals(item.getSeven())
                            || twoClock.equals(item.getEight()) || twoClock.equals(item.getNine())).count();

                    long threeTotal = liveRecordVoList.stream().filter(item -> threeClock.equals(item.getOne()) || threeClock.equals(item.getTwo()) || threeClock.equals(item.getThree())
                            || threeClock.equals(item.getFour()) || threeClock.equals(item.getFive()) || threeClock.equals(item.getSix()) || threeClock.equals(item.getSeven())
                            || threeClock.equals(item.getEight()) || threeClock.equals(item.getNine())).count();

                    long fourTotal = liveRecordVoList.stream().filter(item -> fourClock.equals(item.getOne()) || fourClock.equals(item.getTwo()) || fourClock.equals(item.getThree())
                            || fourClock.equals(item.getFour()) || fourClock.equals(item.getFive()) || fourClock.equals(item.getSix()) || fourClock.equals(item.getSeven())
                            || fourClock.equals(item.getEight()) || fourClock.equals(item.getNine())).count();

                    long fiveTotal = liveRecordVoList.stream().filter(item -> fiveClock.equals(item.getOne()) || fiveClock.equals(item.getTwo()) || fiveClock.equals(item.getThree())
                            || fiveClock.equals(item.getFour()) || fiveClock.equals(item.getFive()) || fiveClock.equals(item.getSix()) || fiveClock.equals(item.getSeven())
                            || fiveClock.equals(item.getEight()) || fiveClock.equals(item.getNine())).count();

                    long sixTotal = liveRecordVoList.stream().filter(item -> sxiClock.equals(item.getOne()) || sxiClock.equals(item.getTwo()) || sxiClock.equals(item.getThree())
                            || sxiClock.equals(item.getFour()) || sxiClock.equals(item.getFive()) || sxiClock.equals(item.getSix()) || sxiClock.equals(item.getSeven())
                            || sxiClock.equals(item.getEight()) || sxiClock.equals(item.getNine())).count();


                    long sevenTotal = liveRecordVoList.stream().filter(item -> sevenClock.equals(item.getOne()) || sevenClock.equals(item.getTwo()) || sevenClock.equals(item.getThree())
                            || sevenClock.equals(item.getFour()) || sevenClock.equals(item.getFive()) || sevenClock.equals(item.getSix()) || sevenClock.equals(item.getSeven())
                            || sevenClock.equals(item.getEight()) || sevenClock.equals(item.getNine())).count();

                    long eightTotal = liveRecordVoList.stream().filter(item -> eightClock.equals(item.getOne()) || eightClock.equals(item.getTwo()) || eightClock.equals(item.getThree())
                            || eightClock.equals(item.getFour()) || eightClock.equals(item.getFive()) || eightClock.equals(item.getSix()) || eightClock.equals(item.getSeven())
                            || eightClock.equals(item.getEight()) || eightClock.equals(item.getNine())).count();

                    long nineTotal = liveRecordVoList.stream().filter(item -> nineClock.equals(item.getOne()) || nineClock.equals(item.getTwo()) || nineClock.equals(item.getThree())
                            || nineClock.equals(item.getFour()) || nineClock.equals(item.getFive()) || nineClock.equals(item.getSix()) || nineClock.equals(item.getSeven())
                            || nineClock.equals(item.getEight()) || nineClock.equals(item.getNine())).count();


                    long tenTotal = liveRecordVoList.stream().filter(item -> tenClock.equals(item.getOne()) || tenClock.equals(item.getTwo()) || tenClock.equals(item.getThree())
                            || tenClock.equals(item.getFour()) || tenClock.equals(item.getFive()) || tenClock.equals(item.getSix()) || tenClock.equals(item.getSeven())
                            || tenClock.equals(item.getEight()) || tenClock.equals(item.getNine())).count();

                    long elevenTotal = liveRecordVoList.stream().filter(item -> elevenClock.equals(item.getOne()) || elevenClock.equals(item.getTwo()) || elevenClock.equals(item.getThree())
                            || elevenClock.equals(item.getFour()) || elevenClock.equals(item.getFive()) || elevenClock.equals(item.getSix()) || elevenClock.equals(item.getSeven())
                            || elevenClock.equals(item.getEight()) || elevenClock.equals(item.getNine())).count();

                    long twelveTotal = liveRecordVoList.stream().filter(item -> twelveClock.equals(item.getOne()) || twelveClock.equals(item.getTwo()) || twelveClock.equals(item.getThree())
                            || twelveClock.equals(item.getFour()) || twelveClock.equals(item.getFive()) || twelveClock.equals(item.getSix()) || twelveClock.equals(item.getSeven())
                            || twelveClock.equals(item.getEight()) || twelveClock.equals(item.getNine())).count();

                    long thirteenTotal = liveRecordVoList.stream().filter(item -> thirteenClock.equals(item.getOne()) || thirteenClock.equals(item.getTwo()) || thirteenClock.equals(item.getThree())
                            || thirteenClock.equals(item.getFour()) || thirteenClock.equals(item.getFive()) || thirteenClock.equals(item.getSix()) || thirteenClock.equals(item.getSeven())
                            || thirteenClock.equals(item.getEight()) || thirteenClock.equals(item.getNine())).count();

                    long fourteenTotal = liveRecordVoList.stream().filter(item -> fourteenClock.equals(item.getOne()) || fourteenClock.equals(item.getTwo()) || fourteenClock.equals(item.getThree())
                            || fourteenClock.equals(item.getFour()) || fourteenClock.equals(item.getFive()) || fourteenClock.equals(item.getSix()) || fourteenClock.equals(item.getSeven())
                            || fourteenClock.equals(item.getEight()) || fourteenClock.equals(item.getNine())).count();


                    long fiveteenTotal = liveRecordVoList.stream().filter(item -> fiveteenClock.equals(item.getOne()) || fiveteenClock.equals(item.getTwo()) || fiveteenClock.equals(item.getThree())
                            || fiveteenClock.equals(item.getFour()) || fiveteenClock.equals(item.getFive()) || fiveteenClock.equals(item.getSix()) || fiveteenClock.equals(item.getSeven())
                            || fiveteenClock.equals(item.getEight()) || fiveteenClock.equals(item.getNine())).count();

                    long sixteenTotal = liveRecordVoList.stream().filter(item -> sixteenClock.equals(item.getOne()) || sixteenClock.equals(item.getTwo()) || sixteenClock.equals(item.getThree())
                            || sixteenClock.equals(item.getFour()) || sixteenClock.equals(item.getFive()) || sixteenClock.equals(item.getSix()) || sixteenClock.equals(item.getSeven())
                            || sixteenClock.equals(item.getEight()) || sixteenClock.equals(item.getNine())).count();

                    long seventeenTotal = liveRecordVoList.stream().filter(item -> sevenTeenClock.equals(item.getOne()) || sevenTeenClock.equals(item.getTwo()) || sevenTeenClock.equals(item.getThree())
                            || sevenTeenClock.equals(item.getFour()) || sevenTeenClock.equals(item.getFive()) || sevenTeenClock.equals(item.getSix()) || sevenTeenClock.equals(item.getSeven())
                            || sevenTeenClock.equals(item.getEight()) || sevenTeenClock.equals(item.getNine())).count();


                    long eighteenTotal = liveRecordVoList.stream().filter(item -> eighteenClock.equals(item.getOne()) || eighteenClock.equals(item.getTwo()) || eighteenClock.equals(item.getThree())
                            || eighteenClock.equals(item.getFour()) || eighteenClock.equals(item.getFive()) || eighteenClock.equals(item.getSix()) || eighteenClock.equals(item.getSeven())
                            || eighteenClock.equals(item.getEight()) || eighteenClock.equals(item.getNine())).count();

                    long nineteenTotal = liveRecordVoList.stream().filter(item -> nineteenClock.equals(item.getOne()) || nineteenClock.equals(item.getTwo()) || nineteenClock.equals(item.getThree())
                            || nineteenClock.equals(item.getFour()) || nineteenClock.equals(item.getFive()) || nineteenClock.equals(item.getSix()) || nineteenClock.equals(item.getSeven())
                            || nineteenClock.equals(item.getEight()) || nineteenClock.equals(item.getNine())).count();

                    long twentyTotal = liveRecordVoList.stream().filter(item -> twentyClock.equals(item.getOne()) || twentyClock.equals(item.getTwo()) || twentyClock.equals(item.getThree())
                            || twentyClock.equals(item.getFour()) || twentyClock.equals(item.getFive()) || twentyClock.equals(item.getSix()) || twentyClock.equals(item.getSeven())
                            || twentyClock.equals(item.getEight()) || twentyClock.equals(item.getNine())).count();

                    long twentyOneTotal = liveRecordVoList.stream().filter(item -> twentyOneClock.equals(item.getOne()) || twentyOneClock.equals(item.getTwo()) || twentyOneClock.equals(item.getThree())
                            || twentyOneClock.equals(item.getFour()) || twentyOneClock.equals(item.getFive()) || twentyOneClock.equals(item.getSix()) || twentyOneClock.equals(item.getSeven())
                            || twentyOneClock.equals(item.getEight()) || twentyOneClock.equals(item.getNine())).count();


                    long twentyTwoTotal = liveRecordVoList.stream().filter(item -> twentyTwoClock.equals(item.getOne()) || twentyTwoClock.equals(item.getTwo()) || twentyTwoClock.equals(item.getThree())
                            || twentyTwoClock.equals(item.getFour()) || twentyTwoClock.equals(item.getFive()) || twentyTwoClock.equals(item.getSix()) || twentyTwoClock.equals(item.getSeven())
                            || twentyTwoClock.equals(item.getEight()) || twentyTwoClock.equals(item.getNine())).count();


                    long twentyThreeTotal = liveRecordVoList.stream().filter(item -> twentyThreeClock.equals(item.getOne()) || twentyThreeClock.equals(item.getTwo()) || twentyThreeClock.equals(item.getThree())
                            || twentyThreeClock.equals(item.getFour()) || twentyThreeClock.equals(item.getFive()) || twentyThreeClock.equals(item.getSix()) || twentyThreeClock.equals(item.getSeven())
                            || twentyThreeClock.equals(item.getEight()) || twentyThreeClock.equals(item.getNine())).count();

                    respItem.setDay(day);
                    respItem.setZeroTotal(zeroTotal);
                    respItem.setOneTotal(oneTotal);
                    respItem.setTwoTotal(twoTotal);
                    respItem.setThreeTotal(threeTotal);
                    respItem.setFourTotal(fourTotal);
                    respItem.setFiveTotal(fiveTotal);
                    respItem.setSixTotal(sixTotal);
                    respItem.setSevenTotal(sevenTotal);
                    respItem.setEightTotal(eightTotal);
                    respItem.setNineTotal(nineTotal);
                    respItem.setTenTotal(tenTotal);
                    respItem.setElevenTotal(elevenTotal);
                    respItem.setTwelveTotal(twelveTotal);
                    respItem.setThirteenTotal(thirteenTotal);
                    respItem.setFourteenTotal(fourteenTotal);
                    respItem.setFiveteenTotal(fiveteenTotal);
                    respItem.setSixteenTotal(sixteenTotal);
                    respItem.setSeventeenTotal(seventeenTotal);
                    respItem.setEighteenTotal(eighteenTotal);
                    respItem.setNineteenTotal(nineteenTotal);
                    respItem.setTwentyTotal(twentyTotal);
                    respItem.setTwentyOneTotal(twentyOneTotal);
                    respItem.setTwentyTwoTotal(twentyTwoTotal);
                    respItem.setTwentyThreeTotal(twentyThreeTotal);
                }
                exportData.add(respItem);
            });
        }
        List<List<LiveNumVo>> export = new ArrayList<>();
        List<LiveNumVo> collect = exportData.stream().sorted(Comparator.comparing(LiveNumVo::getDay).reversed()).collect(Collectors.toList());
        export.add(collect);
        String fileName = "直播人数统计";
        List<String> sheetNames = new ArrayList<>(Collections.singletonList(fileName));
        ArrayList<String> columnName = new ArrayList<>(Arrays.asList("日期","0点", "1点", "2点", "3点", "4点", "5点", "6点", "7点", "8点", "9点", "10点", "11点", "12点", "13点",
                "14点", "15点", "16点", "17点", "18点", "19点", "20点", "21点", "22点", "23点"));
        ExportExcelUtil.exportExcel(fileName, sheetNames, null, columnName,
                export, response, null, true);
    }


}
